package ldh.im.fxbase.richtext;

import javafx.scene.Node;
import javafx.scene.image.ImageView;
import javafx.scene.text.Text;
import javafx.scene.text.TextFlow;
import ldh.im.fxbase.richtext.item.EmojiTextItem;
import ldh.im.fxbase.util.ImRichTextUtil;
import ldh.im.fxbase.util.ParamUtil;

import java.util.List;

public class ImTextField extends TextFlow {

    public ImTextField() {}

    public ImTextField(String content) {
        if (content == null) return;
        List<Object> objects = ImRichTextUtil.split(content);
        for(Object obj : objects) {
            if (obj instanceof TextItem) {
                TextItem textItem = (TextItem) obj;
                Node node = textItem.createNode();
                if (textItem instanceof EmojiTextItem && node instanceof ImageView) {
                    ((ImageView) node).setFitWidth(ParamUtil.MESSAGE_SESSION_EMOJI_SIZE);
                    ((ImageView) node).setFitHeight(ParamUtil.MESSAGE_SESSION_EMOJI_SIZE);
                }
                this.getChildren().add(node);
            } else {
                String s = obj.toString();
                if (s.equals("")) continue;
                this.getChildren().add(new Text(s));
            }
        }
    }
}
